import { RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router';
import { storeToRefs } from 'pinia';
import pinia from '@/store/index';
import { Local } from '@/utils/storage';
import { User } from '@/store/user';
// import { MessageEvent2 } from '@/net/MessageEvent2';
// import { NetMsgType } from '@/netBase/NetMsgType';
const userinfo = User(pinia);
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    meta: { title: 'route_page_home' },
    component: () => import('@/views/home/index.vue'),

  },
  { // 首页头部搜索/收藏
    path: '/searchGames',
    name: 'searchGames',
    meta: { title: 'route_page_search' },
    component: () => import('@/views/searchGames/index.vue'),

  },
  {
    path: '/activity',
    name: 'activity',
    meta: { title: 'route_page_activity' },
    component: () => import('@/views/activity/index.vue'),

  },
  {
    path: '/activityDetail',
    name: 'activityDetail',
    meta: { title: 'route_page_activityDetail' },
    component: () => import('@/views/activityDetail/index.vue'),

  },
  {
    path: '/me',
    name: 'me',
    meta: { title: 'route_page_me' },
    component: () => import('@/views/me/index.vue'),

  },
  {
    path: '/record',
    name: 'record',
    meta: { title: 'route_page_record' },
    component: () => import('@/views/records/index.vue'),
  },
  {
    path: '/login_record',
    name: 'login_record',
    meta: { title: 'route_page_loginRecord' },
    component: () => import('@/views/records/loginRecord.vue'),
  },
  // { // 充值记录
  //   path: '/recharge_record',
  //   name: 'recharge_record',
  //   meta: { title: 'route_page_rechargeRecord' },
  //   component: () => import('@/views/records/rechargeRecord.vue'),
  // },
  // { // 取款记录
  //   path: '/withdraw_record',
  //   name: 'withdraw_record',
  //   meta: { title: 'route_page_withdrawRecord' },
  //   component: () => import('@/views/records/withdrawRecord.vue'),
  // },
  // { // 投注记录
  //   path: '/bet_record',
  //   name: 'bet_record',
  //   meta: { title: 'route_page_betRecord' },
  //   component: () => import('@/views/records/betRecord.vue'),
  // },
  // { // 账变记录
  //   path: '/account_record',
  //   name: 'account_record',
  //   meta: { title: 'route_page_accountRecord' },
  //   component: () => import('@/views/records/accountRecord.vue'),
  // },
  { // 代理账变记录
    path: '/proxy_record',
    name: 'proxy_record',
    meta: { title: 'route_page_proxyRecord' },
    component: () => import('@/views/records/proxyRecord.vue'),
  },
  { // 流水记录
    path: '/audit_record',
    name: 'audit_record',
    meta: { title: 'route_page_auditRecord' },
    component: () => import('@/views/records/auditRecord.vue'),
  },
  { // 洗码记录
    path: '/wash_record',
    name: 'wash_record',
    meta: { title: 'route_page_washRecord' },
    component: () => import('@/views/records/washRecord.vue'),
  },
  {
    path: '/trans',
    name: 'trans',
    meta: { title: 'trans' },
    component: () => import('@/views/trans/index.vue'),

  },
  {
    path: '/vip',
    name: 'vip',
    meta: { title: 'route_page_vip' },
    component: () => import('@/views/vip/index.vue'),
  },
  {
    path: '/vip/rule',
    name: 'vipRule',
    meta: { title: 'route_page_vipLevel' },
    component: () => import('@/views/vip/rules.vue'),
  },
  {
    path: '/deposit',
    name: 'deposit',
    meta: { title: 'route_page_deposit' },
    component: () => import('@/views/deposit/index.vue'),
  },
  {
    path: '/deposit/explain',
    name: 'depositExplain',
    meta: { title: 'deposit_page_instructions' },
    component: () => import('@/views/deposit/explain.vue'),
  },
  {
    path: '/withdrawals',
    name: 'withdrawals',
    meta: { title: 'route_page_withdrawals' },
    component: () => import('@/views/withdrawals/index.vue'),
  },
  {
    path: '/withdrawals/explain',
    name: 'withdrawalsExplain',
    meta: { title: 'withdraw_page_instructions' },
    component: () => import('@/views/withdrawals/explain.vue'),
  },
  {
    path: '/withdrawalNext',
    name: 'withdrawalNext',
    meta: { title: 'route_page_withdrawals' },
    component: () => import('@/views/withdrawals/withdrawalsNext.vue'),
  },
  {
    path: '/myWallet',
    name: 'myWallet',
    meta: { title: 'route_page_wallet' },
    component: () => import('@/views/myWallet/index.vue'),
  },
  {
    path: '/depositNext',
    name: 'depositNext',
    meta: { title: 'route_page_deposit' },
    component: () => import('@/views/deposit/depositNext.vue'),
  },
  {
    path: '/securitySettings',
    name: 'securitySettings',
    meta: { title: 'route_page_securitySettings' },
    component: () => import('@/views/securitySettings/index.vue'),
  },
  {
    path: '/securitySettings/modifyPassWord',  // 修改密码
    name: 'modifyPassWord',
    meta: { title: 'home_page_modifyPassword' },
    component: () => import('@/views/securitySettings/modifyPassWord/index.vue'),
  },
  {
    path: '/securitySettings/modifyWithdraw',  // 修改资金密码
    name: 'modifyWithdraw',
    meta: { title: 'home_page_changeFundPassword' },
    component: () => import('@/views/securitySettings/modifyWithdraw/index.vue'),
  },
  {
    path: '/securitySettings/bindEmail',  // 绑定邮箱
    name: 'bindEmail',
    meta: { title: 'home_page_bindEmail' },
    component: () => import('@/views/securitySettings/bindEmail/index.vue'),
  },
  {
    path: '/securitySettings/bindPhone',  // 绑定手机
    name: 'bindPhone',
    meta: { title: 'home_page_bindPhone' },
    component: () => import('@/views/securitySettings/bindPhone/index.vue'),
  },
  {
    path: '/email', // 我的邮箱
    name: 'email',
    meta: { title: 'route_page_email' },
    component: () => import('@/views/email/index.vue'),
  },
  //会员等级规则
  // {
  //   path: '/me/vip-level',
  //   name: 'vipLevel',
  //   meta: { title: 'route_page_vipLevel' },
  //   component: () => import('@/views/vipLevel/index.vue'),
  // },
  //我的优惠
  {
    path: '/myDiscounts',
    name: 'myDiscounts',
    meta: { title: 'route_page_discounts' },
    component: () => import('@/views/myDiscounts/index.vue'),
  },
  //代理合作
  {
    path: '/agencyCooperation',
    // name: 'agencyCooperation',
    // meta: { title: 'route_page_agencyCooperation' },
    component: () => import('@/views/agencyCooperation/index.vue'),
  },

  //代理介绍
  {
    path: '/agentIntroduction',
    name: 'agentIntroduction',
    meta: { title: 'route_page_agentIntroduction' },
    component: () => import('@/views/agentIntroduction/index.vue'),
  },
  // 支付管理
  {
    path: '/paymentManagement',
    name: 'paymentManagement',
    meta: { title: 'home_page_paymentManagement' },
    component: () => import('@/views/paymentManagement/index.vue'),
  },
  // 支付管理--绑定银行卡
  {
    path: '/paymentManagement/bank',
    name: 'paymentManagementBank',
    meta: { title: 'paymentManagement_page_bank' },
    component: () => import('@/views/paymentManagement/bankList.vue'),
  },
  // 支付管理--绑定USDT
  {
    path: '/paymentManagement/usdt',
    name: 'paymentManagementUsdt',
    meta: { title: 'api_usdt' },
    component: () => import('@/views/paymentManagement/usdtList.vue'),
  },
  // 游戏详情
  {
    path: '/gameDetail',
    name: 'gameDetail',
    meta: { title: 'home_page_gameDetail' },
    component: () => import('@/views/gameDetail/index.vue'),
  },
  // 游戏三级页面
  {
    path: '/game',
    name: 'game',
    meta: { title: 'home_page_game' },
    component: () => import('@/views/game/index.vue'),
  },
  // 聊天列表
  {
    path: '/chatList',
    component: () => import('@/views/chat/chatList.vue'),
  },
  // 好友列表
  {
    path: '/friendsList',
    name: 'friendsList',
    component: () => import('@/views/chat/friendsList.vue'),
  },
  // 聊天室
  {
    path: '/chatRoom',
    name: 'chatRoom',
    component: () => import('@/views/chat/chatRoom/index.vue'),
  },
  // 快捷语设置
  {
    path: '/shortcut',
    name: 'shortcut',
    meta: { title: 'quick_reply_settings' },
    component: () => import('@/views/chat/shortcut/index.vue'),
  },
  {
    path: "/openGame",
    name: "openGame",
    meta: { title: "openGame" },
    component: () => import("@/views/openGame/index.vue"),
  },
  {
    path: "/club",
    name: "club",
    meta: { title: "创建俱乐部" },
    component: () => import("@/views/club/index.vue"),
  },
  {
    path: "/club/create",
    name: "clubCreate",
    meta: { title: "club_page_cjjlb" },
    component: () => import("@/views/club/create.vue"),
  },
  {
    path: "/club/myClub",
    name: "myClub",
    meta: { title: "我的俱乐部" },
    component: () => import("@/views/club/myClub/index.vue"),
  },
  {
    path: "/club/member",
    name: "member",
    meta: { title: "成员管理" },
    component: () => import("@/views/club/member/index.vue"),
  },
  {
    path: "/club/member/approvalMenber",
    name: "approvalMenber",
    meta: { title: "成员审批" },
    component: () => import("@/views/club/member/approvalMenber.vue"),
  },
  {
    path: "/club/member/allMember",
    name: "allMember",
    meta: { title: "全部成员" },
    component: () => import("@/views/club/member/allMember.vue"),
  },
  {
    path: "/club/detail",
    name: "clubDetail",
    meta: { title: "俱乐部详情" },
    component: () => import("@/views/club/clubDetails/index.vue"),
  },
  {
    path: "/club/handList",
    name: "handList",
    meta: { title: "牌局管理" },
    component: () => import("@/views/club/handList/index.vue"),
  },
  {
    path: "/club/myHandList",
    name: "myHandList",
    meta: { title: "我的牌局" },
    component: () => import("@/views/club/myHandList/index.vue"),
  },
  {
    path: "/club/clubHandList",
    name: "clubHandList",
    meta: { title: "牌局列表" },
    component: () => import("@/views/club/clubHandList/index.vue"),
  },
  {
    path: "/club/tournamentList",
    name: "tournamentList",
    meta: { title: "赛事列表" },
    component: () => import("@/views/club/tournamentList/index.vue"),
  },
  {
    path: "/club/eventDetails",
    name: "eventDetails",
    meta: { title: "赛事详情" },
    component: () => import("@/views/club/eventDetails/index.vue"),
  },
  {
    path: "/club/createGamebling",
    name: "createGamebling",
    meta: { title: "创建牌局" },
    component: () => import("@/views/club/createGamebling/index.vue"),
  },

  // 免费夺宝
  {
    path: "/freeLoot",
    name: "freeLoot",
    meta: { title: "free_loot" },
    component: () => import("@/views/freeLoot/index.vue"),
  },

  // 首存
  {
    path: "/firstStorage",
    name: "firstStorage",
    meta: { title: "first_storage" },
    component: () => import("@/views/firstStorage/index.vue"),
  },
  // 免费夺宝
  {
    path: "/category",
    name: "category",
    meta: { title: "" },
    component: () => import("@/views/category/index.vue"),
  },

  // 热门游戏
  {
    path: "/hotGameDetail",
    name: "hotGameDetail",
    meta: { title: "" },
    component: () => import("@/views/hotGameDetail/index.vue"),
    // component: () => import("@/views/freeLoot/index.vue"),
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
});

const waitForCondition = (next: any, isNext: boolean) => {
  const { loadingEnd } = storeToRefs(userinfo);
  return new Promise(async () => {
    let timer
    if (loadingEnd.value) {
      clearTimeout(timer)
      timer = null
      let loadingdom = document.getElementById('loadingCanvas') as any
      loadingdom.style.display = "none"
      if (isNext) {
        next(isNext)
      } else {
        await User(pinia).setLogin(true)
        next('/')
      }

    } else {
      timer = setTimeout(() => {
        console.log(55555);

        waitForCondition(next, isNext);
      }, 200);
    }
  })
}

router.beforeEach(async (to: any, _from: any, next: any) => {
  // 第三方链接进入聊天室
  if (window.location.href.indexOf('chatList') != -1) {
    function getQueryParams(url: string) {
      const paramArr = url.slice(url.indexOf("?") + 1).split("&");
      const params: any = {};
      paramArr.map((param: any) => {
        const [key, val] = param.split("=");
        params[key] = decodeURIComponent(val);
      });
      return params;
    }

    const queryParams = getQueryParams(location.href)
    const deviceId = queryParams.device_id;
    const lang = queryParams.lang
    localStorage.setItem('deviceid', deviceId || '')
    Local.set('lang', lang || 'vn')
    let loadingdom = document.getElementById('loadingCanvas') as any
    loadingdom.style.display = "none"
    return next()
  }

  if (Local.get('user')) {
    if (!['deposit', 'depositNext'].includes(to.name)) {
      Local.remove('curDiscountData');
    }
    await waitForCondition(next, true)
  } else {
    let loadingdom = document.getElementById('loadingCanvas') as any
    loadingdom.style.display = "none"
    if (['home', 'activity', 'me', 'vip', 'activityDetail', 'gameDetail', 'searchGames', 'hotGameDetail'].includes(to.name)) {

      next()
    } else {
      next(false)
      await User(pinia).setLogin(true)

    }
  }


})

router.afterEach(() => {
  try {
    // window.scrollTo({ top: 0, behavior: 'smooth' });
    // window.scroll(0, 0)
  } catch {
    console.error('滚动到顶部失败')
  }
})
export default router;
